<?php


namespace app\app\controller;


use app\app\model\ProjectQualityDisease;
use app\app\model\ProjectTypicalProblems;
use app\BaseController;

class ProjectProblemBeginning extends BaseController
{
    /**
     * 获取初设问题库列表
     * @throws \think\db\exception\DbException
     */
    public function getProjectProblemBeginningList()
    {
        $page_size = $this->request->param('page_size');
        $type = input('type', '1');
        $problem_name = $this->request->param('problem_name');
        $problem_professional = $this->request->param('problem_professional');
        $problem_type = $this->request->param('problem_type');
        $where = [];
        if (!empty($problem_name)) {
            $where[] = ['problem_name', 'like', "%" . $problem_name . "%"];
        }
        if (!empty($problem_professional)) {
            $where[] = ['problem_professional', '=', $problem_professional];
        }
        if (!empty($problem_type)) {
            $where[] = ['problem_type', '=', $problem_type];
        }
        if ($type == 1) {
            $list = \app\app\model\ProjectProblemBeginning::where($where)->paginate($page_size);
        } elseif ($type == 3) {
            $list = ProjectQualityDisease::where($where)->paginate($page_size);
        } elseif ($type == 5) {
            $list = ProjectTypicalProblems::where($where)->paginate($page_size);
        } else {
            $list = [];
        }

        $this->success('success', $list);
    }

    /**
     * 获取搜索下拉框
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProjectProblemSearch()
    {
        $type = input('type', '1');
        $list = [];
        switch ($type) {
            case 1:
                $list['problem_type'] = \app\app\model\ProjectProblemBeginning::field('problem_type')
                    ->group('problem_type')
                    ->select();
                $list['problem_professional'] = \app\app\model\ProjectProblemBeginning::field('problem_professional')
                    ->group('problem_professional')
                    ->select();
                break;
            case 3:
                $list['problem_type'] = ProjectQualityDisease::field('problem_type')
                    ->group('problem_type')
                    ->select();
                $list['problem_professional'] = ProjectQualityDisease::field('problem_professional')
                    ->group('problem_professional')
                    ->select();
                break;
            case 5:
                $list['problem_type'] = ProjectTypicalProblems::field('problem_type')
                    ->group('problem_type')
                    ->select();
                break;
            default:
                $this->error('error');
                break;
        }
        $this->success('success', $list);
    }

    /**
     * 获取问题库详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getProjectProblemInfo()
    {
        $type = input('type', '1');
        $id = $this->request->param('id');
        if (empty($id) || empty($type)) {
            $this->error('error');
        }
        $info = [];
        switch ($type) {
            case 1:
                $info = \app\app\model\ProjectProblemBeginning::where('id', $id)
                    ->find();
                break;
            case 3:
                $info = ProjectQualityDisease::where('id', $id)
                    ->find();
                break;
            case 5:
                $info = ProjectTypicalProblems::where('id', $id)
                    ->find();
                break;
            default:
                $this->error('error');
                break;
        }
        $this->success('success', $info);
    }

}